package com.xxxx.direct.send;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;

/**
 * 路由队列-消息生产者
 */
public class Send {

    // 定义交换机名称
    private final static String EXCHANGE_NAME = "exchange_direct"; // 广播模式

    public static void main(String[] argv) throws Exception {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // rabbitmq服务开启的虚拟机地址
        factory.setHost("192.168.1.105");
        // 登录rabbitmq的用户名、密码
        factory.setUsername("yeb");
        factory.setPassword("yeb");
        // 虚拟地址
        factory.setVirtualHost("/yeb");
        // 端口号
        factory.setPort(5672);

        // 新的写法，没有catch了，在底层代码写了关闭
        try (
                // 连接工厂创建连接
                Connection connection = factory.newConnection();
                // 创建信道
                Channel channel = connection.createChannel()) {
            // 绑定交换机
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
            // 创建消息
            String infoMessage = "普通信息！";
            String errorMessage = "错误信息！";
            String warningMessage = "警告信息！";

            // routingKey
            String infoRoutingKey = "info";
            String errorRoutingKey = "error";
            String warningRoutingKey = "warning";

            // 将产生的消息放入队列
            channel.basicPublish(EXCHANGE_NAME, infoRoutingKey, null, infoMessage.getBytes(StandardCharsets.UTF_8));
            channel.basicPublish(EXCHANGE_NAME, errorRoutingKey, null, errorMessage.getBytes(StandardCharsets.UTF_8));
            channel.basicPublish(EXCHANGE_NAME, warningRoutingKey, null, warningMessage.getBytes(StandardCharsets.UTF_8));

            System.out.println(" [x] Sent '" + infoMessage + "'");
            System.out.println(" [x] Sent '" + errorMessage + "'");
            System.out.println(" [x] Sent '" + warningMessage + "'");

        }
    }
}